package com.anXin.domestic.service.impl;

import cn.hutool.json.JSONUtil;
import com.anXin.apis.user.IUserClient;
import com.anXin.domestic.mapper.DomesticResortMapper;
import com.anXin.domestic.service.DomesticResortService;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.anXin.model.service.dtos.DomesticReleaseDTO;
import com.anXin.model.service.pojos.DomesticResort;
import com.anXin.model.user.dtos.UserInfoDto;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 跑腿服务
 * @author 鹏
 */
@Service
public class DomesticResortServiceImpl extends ServiceImpl<DomesticResortMapper, DomesticResort> implements DomesticResortService {

    @Resource
    private IUserClient iUserClient;

    /**
     * 发布求助帖
     * @param domesticReleaseDTO 前端发送来的数据
     */
    @Override
    public ResponseResult add(DomesticReleaseDTO domesticReleaseDTO) {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        if (user.getId().equals(domesticReleaseDTO.getAuthorId())) {
            DomesticResort domesticResort = new DomesticResort();
            BeanUtils.copyProperties(domesticReleaseDTO, domesticResort);
            domesticResort.setCreatedTime(new Date());
            domesticResort.setState(1);
            domesticResort.setAuthorAvatar(user.getImage());
            domesticResort.setAuthorName(user.getName());
            this.save(domesticResort);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(), "帖子发送成功!");
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH.getCode(), "无权限操作");
        }
    }

    /**
     * 删除求助帖
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Long id) {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        if (this.getById(id).getAuthorId().equals(user.getId())){
            boolean row =  this.removeById(id);
            if (!row){
                return ResponseResult.errorResult(AppHttpCodeEnum.OPERATE_BUSY_ERROR.getCode(),"操作失败,系统繁忙,请稍后重试");
            }
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH.getCode(),"无权限操作");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),"帖子删除成功!");
    }

    /**
     * 审核通过
     * @param id
     * @return
     */
    @Override
    public ResponseResult pass(Long id){
        DomesticResort domesticResort = this.getById(id);
        domesticResort.setState(0);
        this.updateById(domesticResort);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),"帖子已允许发布!");
    }

    /**
     * 审核不通过
     * @param id
     * @return
     */
    @Override
    public ResponseResult fail(Long id){
        DomesticResort domesticResort = this.getById(id);
        domesticResort.setState(2);
        this.updateById(domesticResort);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),"帖子已经退回!");
    }

    /**
     * 展示所有正常发布的求助帖
     * @return
     */
    @Override
    public ResponseResult listNormal() {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        List<DomesticResort> domesticResorts = query().eq("state",0).eq("author_id",user.getId()).list();
        return ResponseResult.okResult(domesticResorts);
    }

    /**
     * 展示所有待审核的求助帖
     * @return
     */
    @Override
    public ResponseResult listReview() {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        List<DomesticResort> domesticResorts = query().eq("state",1).eq("author_id",user.getId()).list();
        return ResponseResult.okResult(domesticResorts);
    }

    /**
     * 展示所有已经撤下或违禁退回的求助帖
     * @return
     */
    @Override
    public ResponseResult listError() {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        List<DomesticResort> domesticResorts = query().eq("state",2).eq("author_id",user.getId()).list();
        return ResponseResult.okResult(domesticResorts);
    }

    /**
     * 展示所有正在订单进行中的求助帖
     * @return
     */
    @Override
    public ResponseResult listOrder() {
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) iUserClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        UserInfoDto user = JSONUtil.toBean(JSONUtil.toJsonStr(data), UserInfoDto.class);
        List<DomesticResort> domesticResorts = query().eq("state",3).eq("author_id",user.getId()).list();
        return ResponseResult.okResult(domesticResorts);
    }

}
